/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbd.crawler.url.fwk;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Amogh
 */
public abstract class BaseURLHandler implements URLHandlerInterface {

    private boolean formatValue = false;

    private String searchRegex  = null;
    private String replaceRegex = null;

    private String originalURL = null;
    
    //--------------------------------------------------------------------------
    /** Javadoc here */
    public BaseURLHandler () { } // end default constructor

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public void setSearchReplace ( String search, String replace ) {

        formatValue = ( isValid (search)
                         && isValid (replace) );

        if ( formatValue ) {

            searchRegex  = search;
            replaceRegex = replace;
        }

    } // end method setSearchReplace

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public String getFormattedValue ( String value ) {

        if ( isFormatValue() ) {

            value = value.replaceAll ( searchRegex, replaceRegex );
        }

        return value;

    } // end method setSearchReplace

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public boolean isFormatValue () {
        return formatValue;
    } // end method isFormatValue
    
    //--------------------------------------------------------------------------
    /** Javadoc here */
    protected ArrayList parseForTokens () {
        
        ArrayList tokenList = null;

        if ( getOriginalURL() != null ) {

            final String REGEX_TOKEN = "#\\{[0-9]+\\}";

            Pattern pattern = Pattern.compile ( REGEX_TOKEN );

            Matcher matcher = pattern.matcher ( getOriginalURL() );

            do {
                
                if ( matcher.find() ) {

                    if ( tokenList == null ) {
                        tokenList = new ArrayList ();
                    }

                    tokenList.add ( matcher.group().trim() );
                }
            }
            while ( !matcher.hitEnd() );
        }
        else {
            throw new RuntimeException ( 
                        "{BaseURLHandler.parseForTokens} expecting original url to be set before parsing."
                   );
        }

        return tokenList;

    } // end method parseForTokens

    //--------------------------------------------------------------------------
    /**
     * @return the originalURL
     */
    protected String getOriginalURL () {
        return originalURL;
    }

    /**
     * @param originalURL the originalURL to set
     */
    protected void setOriginalURL (String url) {
        this.originalURL = url;
    }

    //--------------------------------------------------------------------------
    /** Javadoc here */
    private boolean isValid ( String value ) {
        return ( value != null && value.length() > 0 );
    } // end method isValid

} // end class BaseURLHandler
